/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hu.bme.j2ee.v37zen.beans.util;

import com.google.gson.Gson;
import hu.bme.j2ee.v37zen.beans.DialManagerBean;
import hu.bme.j2ee.v37zen.beans.FindUsersBean;
import hu.bme.j2ee.v37zen.beans.RegisterUserBean;
import hu.bme.j2ee.v37zen.entities.User;
import hu.bme.j2ee.v37zen.util.PasswordDigest;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import javax.ejb.Startup;

/**
 *
 * @author Kiss Dániel
 */
@Singleton
@Startup
public class StartupBean {
    
    @EJB
    FindUsersBean fub;
    @EJB
    RegisterUserBean rub;
    @EJB
    CleanupBean clb;
    
    @EJB
    DialManagerBean dmb;
    
    @PostConstruct
    public void postConstruct(){
        
        System.out.println("[SD] Startup!");
        
        clb.removeAllUser();
        
        if(fub.isUserTableEmpty()){
            
            User dani = rub.createUser("dani", "kissdani89@gmail.com", PasswordDigest.encodePasswordUnsafe("password"));
            rub.createAdministrator("admin", "nass.diki@gmail.com", PasswordDigest.encodePasswordUnsafe("admin"));
            
            try {
                dmb.createAndAddDial(dani, new URL("http://google.com"), 0);
                dmb.createAndAddDial(dani, new URL("http://www.w3schools.com/"), 1);
                dmb.createAndAddDial(dani, new URL("http://stackoverflow.com/questions/15828540/" +
                        "programmatically-create-and-add-composite-component-in-backing-bean"), 2);
                
                
            } catch (MalformedURLException ex) {
                
            }
            
        }
      
        
        List<User> users = fub.findAllUser();
        for (User user : users) {
            System.out.println("[SD] " + user.toString());
            
            Gson gson = new Gson();
            System.out.println(gson.toJson(user).toString());
            
        }
        
        
        
        
        
//        println("By name:");
//        User u1 = fub.findUserByUsername("dani");
//        User u2 = fub.findUserByUsername("admin");
//        User u3 = fub.findUserByUsername("béla");
//        
//        System.out.println("[SD] " + u1);
//        System.out.println("[SD] " + u2);
//        System.out.println("[SD] " + u3);
//        
//        println("By email:");
//        u1 = fub.findUserByEmail("kissdani89@gmail.com");
//        u2 = fub.findUserByEmail("nass.diki@gmail.com");
//        u3 = fub.findUserByEmail("kissdani@fastmail.net");
//        
//        System.out.println("[SD] " + u1);
//        System.out.println("[SD] " + u2);
//        System.out.println("[SD] " + u3);
//        
//        List<User> ul1 = fub.findUserByRoleSet(Role.SET_of_USER_ONLY);
//        List<User> ul2 = fub.findUserByRoleSet(Role.SET_of_ADMIN_ONLY);
//        List<User> ul3 = fub.findUserByRoleSet(Role.SET_of_USDER_AND_ADMIN);
//        
//        println("By roles:");
//        println("Users:");
//        for (User user : ul1) {
//            println(user);
//        }
//        
//        println("Admins:");
//        for (User user : ul2) {
//            println(user);
//        }
//        
//        println("Users & Admins:");
//        for (User user : ul3) {
//            println(user);
//        }
        
        
        System.out.println("[SD] Startedup!");
    }


    
    private void println(Object o){
        System.out.println(o);
    }
    
    private void println(String s){
        System.out.println(s);
    }
    
}
